package com.jvtd.eyl.ui.main;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.jvtd.eventBus.EventCenter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BaseMvpActivity;
import com.jvtd.eyl.bean.eventBus.MainBarBean;
import com.jvtd.eyl.bean.ui.BottomBarBean;
import com.jvtd.eyl.constants.EventCode;
import com.jvtd.eyl.ui.main.bookcase.BookcaseFragment;
import com.jvtd.eyl.ui.main.home.HomeFragment;
import com.jvtd.eyl.ui.main.my.MyFragment;
import com.jvtd.eyl.utils.AppIndicatorUtils;
import com.jvtd.bean.BarData;
import com.jvtd.utils.SystemUtils;
import com.jvtd.widget.viewPager.JvtdViewPager;
import com.jvtd.widget.viewPager.pagerAdapter.JvtdFragmentPagerAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
/**
 * 首页
 *
 * 作者:chenlei
 * 时间:2018/11/12 3:17 PM
 */
public class MainActivity extends BaseMvpActivity {

    // 再点一次退出程序时间
    private static final long WAIT_TIME = 2000L;

    @BindView(R.id.view_pager)
    JvtdViewPager viewPager;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;

    private long TOUCH_TIME = 0;

    private int mSelectIndex;

    public static Intent getIntent(Context context) {
        return new Intent(context, MainActivity.class);
    }

    @Override
    protected void initInject() {

    }

    @Override
    public boolean isImmersionBar()
    {
        return true;
    }

    @Override
    public boolean setStatusBarTextBlack()
    {
        return false;
    }

    @Override
    public int setStatusBarColor()
    {
        return R.color.color_empty;
    }

    @Override
    protected void getLayout() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initViewAndData() {
        List<BarData> bottomBarBeans = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        bottomBarBeans.add(new BottomBarBean(getString(R.string.bookcase_title), R.drawable.icon_bookcase_false, R.drawable.icon_bookcase_true));
        fragments.add(BookcaseFragment.newInstance());
        bottomBarBeans.add(new BottomBarBean(getString(R.string.home_title), R.drawable.icon_choiceness_false, R.drawable.icon_choiceness_true));
        fragments.add(HomeFragment.newInstance());
        bottomBarBeans.add(new BottomBarBean(getString(R.string.my_title), R.drawable.icon_my_false, R.drawable.icon_my_true));
        fragments.add(MyFragment.newInstance());

        JvtdFragmentPagerAdapter adapter = new JvtdFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(fragments.size());

        AppIndicatorUtils.initBarMagicIndicator(mContext, viewPager, magicIndicator, bottomBarBeans,
                R.color.color_78ffffff, R.color.color_fff, index -> mSelectIndex = index);

        mSelectIndex = 1;
        viewPager.setCurrentItem(mSelectIndex,false);
    }

    @Override
    public void onBackPressedSupport() {
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
            SystemUtils.returnDesktop(mContext);
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            showMessage(R.string.press_again_back_home);
        }
    }

    /**
     * 沉浸式效果
     * @param isImmersion 是否开启
     * @param statusBarDarkFont 是否需要黑字体
     */
    public void setStatusBarParams(boolean isImmersion,boolean statusBarDarkFont)
    {
        int statusBarColor;
        if (ImmersionBar.isSupportStatusBarDarkFont())
            statusBarColor = R.color.color_fff;
        else
            statusBarColor = R.color.color_000;
        if (isImmersion)
            statusBarColor = R.color.color_empty;
        getBar().statusBarColor(statusBarColor).statusBarDarkFont(statusBarDarkFont).init();
    }

    /**
     * 是否隐藏底部导航
     * @param hide 是否隐藏
     */
    public void hideNavigation(boolean hide){
        magicIndicator.setVisibility(hide ? View.GONE : View.VISIBLE);
    }

    @Override
    public void activityOnEvent(EventCenter eventCenter) {
        super.activityOnEvent(eventCenter);
        if (eventCenter.getEventCode() == EventCode.UPDATE_BAR){
            if (eventCenter.getData() instanceof MainBarBean){
                MainBarBean mainBarBean = (MainBarBean) eventCenter.getData();
                setStatusBarParams(mainBarBean.isImmersion(),mainBarBean.isDark());
            }
        }
    }
}
